import * as React from 'react'
import axios from 'axios'
import Avatar from '@mui/material/Avatar'
import Button from '@mui/material/Button'
import CssBaseline from '@mui/material/CssBaseline'
import TextField from '@mui/material/TextField'
import FormControlLabel from '@mui/material/FormControlLabel'
import Checkbox from '@mui/material/Checkbox'
import Link from '@mui/material/Link'
import Grid from '@mui/material/Grid'
import Box from '@mui/material/Box'
import LockOutlinedIcon from '@mui/icons-material/LockOutlined'
import Typography from '@mui/material/Typography'
import Container from '@mui/material/Container'
import { createTheme, ThemeProvider } from '@mui/material/styles'

const instance = axios.create({
    baseURL: 'http://172.24.56.192:5000/start/login', // 后端服务的地址
    timeout: 150, // 请求超时时间
  })

function Copyright(props) {
    return (
        <Typography
            variant="body2"
            color="text.secondary"
            align="center"
            {...props}
        >
            {'Copyright © '}
            <Link color="inherit" href="https://mui.com/">
                Your Website
            </Link>{' '}
            {new Date().getFullYear()}
            {'.'}
        </Typography>
    )
}

// TODO remove, this demo shouldn't need to reset the theme.

const defaultTheme = createTheme()

export default function SignIn() {
    const handleSubmit = event => {
        event.preventDefault()
        const data = new FormData(event.currentTarget)
        console.log({
            email: data.get('email'),
            password: data.get('password'),
        })
        axios.post('http://172.24.56.192:5000/start/login',data,{headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
            }})
              .then(function (response) {
                  // 这里是处理响应的代码
                 // var code = response['code']
                  if(response.data['code'] == 200)
                  {
                    window.location.href="AppHome_Aut"
                  }
                  else{
                    if(response.data['code'] == 100)
                    {
                        window.location.href="SignIn"
                        alert('请填写账号和密码!')
                    }
                    else{
                        if(response.data['code'] == 300)
                        {
                            window.location.href="SignIn"
                            alert('请填写账号!')
                        }
                        else{
                            if(response.data['code'] == 400)
                            {
                                window.location.href="SignIn"
                                alert('请填写密码!')
                            }
                            else{
                                if(response.data['code'] == 500)
                                {
                                    window.location.href="SignIn"
                                    alert('账号和密码格式有误!密码长度应为6-20!')
                                }
                                else{
                                    if(response.data['code'] == 600)
                                    {
                                       window.location.href="SignIn"
                                       alert('账号未注册!')
                                    }
                                    else{
                                        if(response.data['code'] == 700)
                                        {
                                            window.location.href="SignIn"
                                            alert('密码错误!')
                                        }
                                    }
                                }
                            }
                        }
                    }
                  }
                  console.log(response);
                  
              })
              .catch(function (error) {
                  // 这里是处理错误的代码
                 // window.location.href="https://www.baidu.com/"
                  console.log(error);
                 
              })
    }

    

    return (
        <ThemeProvider theme={defaultTheme}>
            <Container component="main" maxWidth="xs">
                <CssBaseline />
                <Box
                    sx={{
                        marginTop: 8,
                        display: 'flex',
                        flexDirection: 'column',
                        alignItems: 'center',
                    }}
                >
                    <img
                        //style={{ width: 600, height: 500 }}
                        src="\微信图片_20230523231821.jpg"
                        alt=""
                        className="absolute inset-0 -z-10 h-full w-full object-cover object-right md:object-center"
                    />
                    <Avatar sx={{ m: 1, bgcolor: 'secondary.main' }}>
                        <LockOutlinedIcon />
                    </Avatar>
                    <Typography component="h1" variant="h5">
                        Sign in
                    </Typography>
                    <Box
                        component="form"
                        onSubmit={handleSubmit}
                        noValidate
                        sx={{ mt: 1 }}
                    >
                        <TextField
                            margin="normal"
                            required
                            fullWidth
                            id="email"
                            label="Email Address"
                            name="email"
                            autoComplete="email"
                            autoFocus
                        />
                        <TextField
                            margin="normal"
                            required
                            fullWidth
                            name="password"
                            label="Password"
                            type="password"
                            id="password"
                            autoComplete="current-password"
                        />
                        <FormControlLabel
                            control={
                                <Checkbox value="remember" color="primary" />
                            }
                            label="Remember me"
                        />
                        <Button
                            type="submit"
                            fullWidth
                            variant="contained"
                            sx={{ mt: 3, mb: 2 }}
                            className="text-black"
                            
                            //href="/AppHome_Aut"
                        >
                            Sign In
                        </Button>
                        <Grid container>
                            <Grid item xs>
                                <Link href="#" variant="body2"></Link>
                            </Grid>
                            <Grid item>
                                <Link href="SignUp" variant="body2">
                                    {"Don't have an account? Sign Up"}
                                </Link>
                            </Grid>
                        </Grid>
                    </Box>
                </Box>
                <Copyright sx={{ mt: 8, mb: 4 }} />
            </Container>
        </ThemeProvider>
    )
}
